{extend name="app/admin/view/base.html"/}
{block name="resources"}
{/block}
{block name="main"}

<!-- 搜索框 -->
<div class="ns-single-filter-box">
	<button class="layui-btn ns-bg-color" onclick="addGroup()">添加笔记分组</button>
	<div class="layui-form">
		<div class="layui-input-inline">
			<input type="text" name="group_name" placeholder="请输入笔记分组名称" autocomplete="off" class="layui-input">
			<button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
				<i class="layui-icon">&#xe615;</i>
			</button>
		</div>
	</div>
</div>

<table id="attr_class_list" lay-filter="attr_class_list"></table>

<!-- 编辑排序 -->
<script type="text/html" id="editSort">
	<input name="sort" type="number" onchange="editSort({{d.group_id}}, this)" value="{{d.sort}}" class="layui-input edit-sort ns-len-short">
</script>

<!-- 操作 -->
<script type="text/html" id="operation">
	<div class="ns-table-btn">
		<a class="layui-btn" lay-event="edit">编辑</a>
		<a class="layui-btn" lay-event="delete">删除</a>
	</div>
</script>

<script type="text/html" id="addGroup">

	<div class="layui-form">
		<div class="layui-form-item">
			<label class="layui-form-label mid"><span class="required">*</span>笔记分组：</label>
			<div class="layui-input-block">
				<input name="group_name" type="text" placeholder="请输入笔记分组名称" lay-verify="required" class="layui-input ns-len-mid">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label mid">排序：</label>
			<div class="layui-input-block">
				<input name="sort" type="number" class="layui-input edit-sort ns-len-short">
			</div>
		</div>

		<div class="ns-form-row mid">
			<button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
			<button class="layui-btn layui-btn-primary" onclick="closeAttrLayer()">返回</button>
		</div>
	</div>

</script>

<script type="text/html" id="editGroup">

	<div class="layui-form">
		<div class="layui-form-item">
			<label class="layui-form-label mid"><span class="required">*</span>笔记分组：</label>
			<div class="layui-input-block">
				<input name="group_name" type="text" value="{{ d.group_name }}" placeholder="请输入笔记分组名称" lay-verify="required" class="layui-input ns-len-mid">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label mid">排序：</label>
			<div class="layui-input-block">
				<input name="sort" type="number" value="{{ d.sort }}" class="layui-input edit-sort ns-len-short">
			</div>
		</div>
		<input type="hidden" name="group_id" value="{{ d.group_id }}">
		<div class="ns-form-row mid">
			<button class="layui-btn ns-bg-color" lay-submit lay-filter="edit_save">保存</button>
			<button class="layui-btn layui-btn-primary" onclick="closeAttrLayer()">返回</button>
		</div>
	</div>

</script>

{/block}
{block name="script"}
<script>
    var laytpl, add_attr_index = -1,
        form, table;
    layui.use(['form', 'laytpl'], function() {
        var repeat_flag = false; //防重复标识
        laytpl = layui.laytpl;
        form = layui.form;
        form.render();

        table = new Table({
            elem: '#attr_class_list',
            url: ns.url("notes://admin/group/lists"),
            cols: [
                [ {
                    field: 'group_name',
                    title: '分组名称',
                    unresize: 'false'
                },{
                    field: 'notes_num',
                    title: '店铺笔记总数',
                    unresize: 'false'
                },{
                    field: 'release_num',
                    title: '笔记发布数',
                    unresize: 'false'
                },{
                    unresize:'false',
                    title: '排序',
                    width: '20%',
                    align: 'center',
                    templet: '#editSort'
                },{
                    title: '操作',
                    width: '15%',
                    toolbar: '#operation',
                    unresize: 'false'
                }]
            ]
        });

        /**
         * 监听工具栏操作
         */
        table.tool(function(obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    editGroup(data);
                    break;
                case 'delete':
                    deleteGroup(data.group_id);
                    break;
            }
        });

        /**
         * 删除
         */
        function deleteGroup(group_id) {
            layer.confirm('确认删除该分组吗？', function() {
                $.ajax({
                    url: ns.url("notes://admin/group/delete"),
                    data: {
                        group_id:group_id
                    },
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg(res.message);
                        if (res.code == 0) {
                            table.reload();
                        }
                    }
                });
            });
        }

        /**
         * 搜索功能
         */
        form.on('submit(search)', function(data) {
            table.reload({
                page: {
                    curr: 1
                },
                where: data.field
            });
        });

        form.on('submit(save)', function(data) {

            if (repeat_flag) return false;
            repeat_flag = true;

            $.ajax({
                url: '{:addon_url("notes://admin/group/add")}',
                data: data.field,
                dataType: 'JSON',
                type: 'POST',
                success: function(data) {
                    layer.msg(data.message);
                    if (data.code == 0) {
                        table.reload();
                        layer.close(add_attr_index);
                    }
                    repeat_flag = false;
                }
            });
            return false;
        });

        form.on('submit(edit_save)', function(data) {

            if (repeat_flag) return false;
            repeat_flag = true;

            $.ajax({
                url: '{:addon_url("notes://admin/group/edit")}',
                data: data.field,
                dataType: 'JSON',
                type: 'POST',
                success: function(data) {
                    layer.msg(data.message);
                    if (data.code == 0) {
                        table.reload();
                        layer.close(add_attr_index);
                    }
                    repeat_flag = false;
                }
            });
            return false;
        });

        /**
         * 表单验证
         */
        form.verify({
            num: function(value) {
                if (value == '') {
                    return;
                }
                if (value % 1 != 0) {
                    return '排序数值必须为整数';
                }
                if (value < 0) {
                    return '排序数值必须为大于0';
                }
            }
        });
    });

    function addGroup() {
        var add_attr = $("#addGroup").html();
        laytpl(add_attr).render({}, function(html) {
            add_attr_index = layer.open({
                title: '添加笔记分组',
                skin: 'layer-tips-class',
                type: 1,
                area: ['500px'],
                content: html
            });
        });

    }

    function editGroup(data) {
        var add_attr = $("#editGroup").html();
        laytpl(add_attr).render(data, function(html) {
            add_attr_index = layer.open({
                title: '编辑笔记分组',
                skin: 'layer-tips-class',
                type: 1,
                area: ['500px'],
                content: html
            });
        });

    }

    function closeAttrLayer() {
        layer.close(add_attr_index);
    }

    // 监听单元格编辑
    function editSort(group_id, event){
        var data = $(event).val();

        if (data == '') {
            $(event).val(0);
            data = 0;
        }

        if(!new RegExp("^-?[0-9]\\d*$").test(data)){
            layer.msg("排序号只能是整数");
            return ;
        }
        if(data<0){
            layer.msg("排序号必须大于0");
            return ;
        }
        $.ajax({
            type: 'POST',
            url: ns.url("notes://admin/group/modifySort"),
            data: {
                sort: data,
                group_id: group_id
            },
            dataType: 'JSON',
            success: function(res) {
                layer.msg(res.message);
                if(res.code==0){
                    table.reload();
                }
            }
        });
    }
</script>
{/block}